package cn.edu.klmy.service;


import cn.edu.klmy.dao.IAuthenticationDao;
import cn.edu.klmy.dao.IAuthorityDao;
import cn.edu.klmy.models.user.AuthenticationUser;
import cn.edu.klmy.spring.security.core.userdetails.CourseSystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: amethyst
 * Date: 13-2-5
 * Time: 下午6:03
 * To change this template use File | Settings | File Templates.
 */
@Service
public class AuthenticationService implements UserDetailsService {
    @Autowired
    private IAuthenticationDao authenticationDao;

    @Autowired
    private IAuthorityDao authorityDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        AuthenticationUser authenticationUser = authenticationDao.getUserByUserName(s);

        List<Integer> roleIds = authorityDao.getRoleIdsByUserID(authenticationUser.getUserId());
        if (authenticationUser == null) {
            return null;
        }
        return new CourseSystemUser(authenticationUser, roleIds);
    }


}
